package panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import recipe.Ingredient;

@SuppressWarnings("serial")
public class IngredientRow extends JPanel implements ActionListener{
	private JTextField txtAmount;
	private JTextField txtName;
	private JComboBox<String> units;
	private JButton btnDelete;
	private JPanel rows;
	private IngredientPanel container;
	private JScrollPane scroller;
	private ArrayList<IngredientRow> list;
	
	/**
	 * Constructor
	 * @param container
	 * @param scroller
	 * @param rows
	 * @param list
	 */
	public IngredientRow(IngredientPanel container, JScrollPane scroller, JPanel rows, ArrayList<IngredientRow> list){
		this.container = container;
		this.rows = rows;
		this.list = list;
		this.scroller = scroller;
		
		units = new JComboBox<String>(new DefaultComboBoxModel<String>(Ingredient.getUnits()));
		units.setEditable(false);
		
		txtAmount = new JTextField(3);
		txtName = new JTextField(20);
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(this);
		
		this.add(txtAmount);
		this.add(units);
		this.add(txtName);
		this.add(btnDelete);
	}
	
	/**
	 * Method - getIngredient
	 * Description - Creates an ingredient object from the data
	 * entered into the fields
	 * @return Ingredient - Returns if fields have the proper values
	 * @return null - Returns if the fields are missing values or incorrect
	 */
	public Ingredient getIngredient(){
		Ingredient ing = null;
		if(validateFields()){
			ing = new Ingredient(txtName.getText(), txtAmount.getText(), (String) units.getSelectedItem());
		}
		
		return ing;
	}
	
	public String getIngredientAsString(){
		StringBuilder temp = new StringBuilder();
		temp.append(txtAmount.getText() + " ");
		temp.append((String) units.getSelectedItem() + " ");
		temp.append(txtName.getText());
		return temp.toString();
	}
	
	/**
	 * Method - setIngredient
	 * Description - Sets the fields of the row with
	 * the data from the ingredient object
	 * @param ing
	 * @return void
	 */
	public void setIngredient(Ingredient ing){
		txtName.setText(ing.getIngredientName());
		txtAmount.setText(ing.getAmount());
		units.setSelectedItem(ing.getUnit());
	}
	
	/**
	 * Method - getRowNumber
	 * Description - Getter for the row number
	 * @return int - the row number
	 */
	public int getRowNumber(){
		return list.indexOf(this);
	}
	
	/**
	 * Method - getName
	 * Description - Getter for the text in the name field
	 * @return String - the text in the name field
	 */
	public String getName(){
		return txtName.getText();
	}
	
	/**
	 * Method - setName
	 * Description - Setter for name field
	 * @param name - the name as a String
	 * @return void
	 */
	public void setName(String name){
		txtName.setText(name);
	}
	
	/**
	 * Method - getAmount
	 * Description - Getter for the text in the amount field
	 * @return String - the text in the amountfield
	 */
	public String getAmount(){
		return txtAmount.getText();
	}
	
	/**
	 * Method - actionPerformed
	 * Description - ActionListener method for the delete button
	 * Removes the row from the view and arraylist.
	 * @return void
	 */
	public void actionPerformed(ActionEvent e){
		// Remove row from view
		rows.remove(IngredientRow.this);
		// Remove row from list
		list.remove(IngredientRow.this);
		// Decrement row count
		container.decrementRowCount();
		// Refresh view
		rows.repaint();
		scroller.revalidate();
	}
	
	/**
	 * Method - validateFields
	 * Description - Validates the data in the input fields
	 * @return True - Returns if data in fields are valid
	 * @return False - Returns if data in fields are invalid
	 */
	private boolean validateFields(){
		return 
			!txtName.getText().isEmpty() &&
			!txtAmount.getText().isEmpty() &&
			(Ingredient.parseAmount(txtAmount.getText()) > 0);
	}
}
